package com.zndroid.gridview.preview;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.zndroid.gridview.AGridImageViewAdapter;
import com.zndroid.gridview.GridImageViewLayout;
import com.zndroid.gridview.IImageBean;
import com.zndroid.gridview.ImagePreviewActivity;
import com.zndroid.gridview.inner.ScreenUtil;
import com.zndroid.gridview.preview.indicator.impl.RoundLinesIndicator;

import java.util.ArrayList;
import java.util.List;

public class GridImageViewClickAdapter<T extends IImageBean> extends AGridImageViewAdapter<T> {
    private final int statusHeight;

    public GridImageViewClickAdapter(Context context, List<T> imageInfo) {
        super(context, imageInfo);
        statusHeight = new ScreenUtil().getStatusHeight(context);
    }

    @Override
    protected void onImageItemClick(Context context, ImageView imageView, GridImageViewLayout gridImageViewLayout, int index, List<T> imageInfoList) {
        if (null == imageInfoList || imageInfoList.size() <= 0) {return;}

        int len = imageInfoList.size();
        List<ImageAttribute> imageAttributes = new ArrayList<>(4);

        for (int i = 0; i < len; i++) {
            T info = imageInfoList.get(i);
            View view;
            if (i < gridImageViewLayout.getMaxSize()) {
                view = gridImageViewLayout.getChildAt(i);
            } else {
                //如果图片的数量大于显示的数量，则超过部分的返回动画统一退回到最后一个图片的位置
                view = gridImageViewLayout.getChildAt(gridImageViewLayout.getMaxSize() - 1);
            }

            int[] w_h_x_y = new int[4];
            w_h_x_y[0] = view.getWidth();
            w_h_x_y[1] = view.getHeight();
            int[] points = new int[2];
            imageView.getLocationInWindow(points);
            w_h_x_y[2] = points[0];
            w_h_x_y[3] = points[1] - statusHeight;

            imageAttributes.add(new ImageAttribute(w_h_x_y, info));
        }

        ImagePreviewActivity.toPreview(context, gridImageViewLayout.getImageLoader(), imageAttributes, index);

    }
}
